<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="来源系统" prop="sysCode">
        <el-select v-model="queryParams.sysCode" placeholder="请选择" clearable class="!w-120px">
          <el-option
            v-for="dict in sysCodes"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="keyWord">
        <el-input placeholder="请输入内容" v-model.trim="keyWord">
          <template #prepend>
            <el-select v-model="kType" placeholder="请选择" class="!w-120px">
              <el-option label="设备MAC" value="devSn" />
              <el-option label="设备名称" value="devName" />
              <el-option label="租户手机号" value="empPhone" />
              <el-option label="租户姓名" value="empName" />
            </el-select>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="来源系统" align="center" prop="sysCode" width="100"/>
      <el-table-column label="社区名称" align="center" prop="communityName" width="120"/>
      <el-table-column label="设备MAC" align="center" prop="devSn" width="100"/>
      <el-table-column label="设备名称" align="center" prop="devName" width="120"/>
      <el-table-column label="租户ID" align="center" prop="empId" width="80"/>
      <el-table-column label="租户手机号" align="center" prop="empPhone" width="120"/>
      <el-table-column label="租户姓名" align="center" prop="empName" width="120"/>
      <el-table-column label="卡号" align="center" prop="empCardNo" width="120"/>
      <el-table-column label="抓拍图片地址" align="center" prop="captureImageUrl" />
      <el-table-column label="人脸识别" align="center" >
        <el-table-column label="年龄" align="center" prop="faceAge" width="60"/>
        <el-table-column label="性别" align="center" prop="faceGender" width="60"/>
        <el-table-column label="比对" align="center" prop="faceMatchScore" width="60"/>
        <el-table-column label="体温" align="center" prop="bodyTemperature" width="60"/>
      </el-table-column>
      <el-table-column label="密码或二维码" align="center" prop="number" width="100"/>
      <el-table-column label="推送事件种类" align="center" prop="eventKinds" width="120"/>
      <el-table-column label="记录时间" align="center" prop="eventTime" width="120"/>
      <el-table-column label="事件类型 " align="center" prop="eventType" width="100"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['zwsj:open-record:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { OpenRecordApi, OpenRecordVO } from '@/api/zwsj/openrecord'
/** 开门日志 列表 */
defineOptions({ name: 'OpenRecord' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OpenRecordVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)

const keyWord = ref('')
const kType = ref('devName')
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  sysCode: undefined,
  communityId: undefined,
  communityName: undefined,
  devSn: '',
  devName: '',
  empPhone: '',
  empName: '',
  eventKinds: undefined,
})

const sysCodes = reactive([
  {
    value: 'ximo',
    label: '西墨'
  },
  {
    value: 'ttl',
    label: '通通锁'
  }
])

const makeKeyword = async () =>{
  queryParams.devSn = ''
  queryParams.devName = ''
  queryParams.empPhone = ''
  queryParams.empName= ''
  if (kType.value == 'devSn') {
    queryParams.devSn = keyWord.value
  } else if (kType.value == 'devName') {
    queryParams.devName = keyWord.value
  }else if (kType.value == 'empPhone') {
    queryParams.empPhone = keyWord.value
  }else if (kType.value == 'empName') {
    queryParams.empName = keyWord.value
  }
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    makeKeyword()
    //devSn  devName  empPhone empName
    const data = await OpenRecordApi.getOpenRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OpenRecordApi.deleteOpenRecord(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>